<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="版本名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入版本名称"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <div>
          <a-row v-for="(item1, index1) in model.priceBatchList" :key="index1">
            <a-col :span="8">
              <a-form-model-item label="年限" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cycle">
                <a-input v-model="item1.cycle" placeholder="请输入年限"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="价格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="price">
                <a-input v-model="item1.price" placeholder="请输入价格"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="原价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="originalPrice">
                <a-input v-model="item1.originalPrice" placeholder="请输入原价"></a-input>
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-button v-if="!formDisabled" @click="addpriceBatchList" icon="plus-circle" style="
              margin: 0 18px 20px;
              width: calc(100% - 36px);
              background: #e6f7ff;
              border: 1px solid #91d5ff;
              color: #1890ff;
            ">新增</a-button>
        </div>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'XghAuthorizationCenterPriceForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        name: [
          { required: true, message: '请输入版本名称!' },
        ],
      },
      url: {
        add: "/XghAuthorizationCenter/xghAuthorizationCenterPrice/add",
        edit: "/XghAuthorizationCenter/xghAuthorizationCenterPrice/edit",
        queryById: "/XghAuthorizationCenter/xghAuthorizationCenterPrice/queryById"
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      if (!this.model.priceBatchList) {
        this.$set(this.model, 'priceBatchList', [])
      }
      this.visible = true;
    },
    addpriceBatchList() {
      this.model.priceBatchList.push({})
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          let goon = true
          if (!that.model.priceBatchList || that.model.priceBatchList.length == 0) {
            goon = false
          } else {
            for (let index = 0; index < that.model.priceBatchList.length; index++) {
              const element = that.model.priceBatchList[index];
              if (!element.cycle || !element.price || !element.originalPrice || parseFloat(element.cycle) == 0 || parseFloat(element.price) == 0 || parseFloat(element.originalPrice) == 0) {
                goon = false
              }
            }
          }

          if (goon == false) {
            that.$elMessage.warning('请填写完整的周期或价格');
            return
          }

          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$elMessage.success(res.message);
              that.$emit('ok');
            } else {
              that.$elMessage.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  }
}
</script>